<?php

require_once 'BaseMapper.php';
class Application_Model_UsuarioGranjaMapper extends BaseMapper
{
    public function __construct()
    {
        parent::setDbTable(new Application_Model_DbTable_UsuarioGranja());
        parent::setExtendedModel(new Application_Model_UsuarioGranja());
    }

    public function fetchAll()
    {
        $table = Zend_Db_Table::getDefaultAdapter();
        $select = $table->select()
            ->from(array('ug' => 'usuario_has_granja'), array('*'))
            ->joinInner(array('g' => 'granja'), "ug.granja_id = g.id", array('nombre'))
            ->joinInner(array('u' => 'usuario'), "ug.usuario_id = u.id", array('correo'));

        $resultSet = $table->fetchAll($select);

        $entries = array();
        foreach ($resultSet as $row) {
            $mapped = $this->map($row);
            $mapped->addOtherColumn(array("granja" => $row['nombre']));
            $mapped->addOtherColumn(array("usuario" => $row['correo']));
            $entries[] = $mapped;
        }
        return $entries;
    }

    public function fetchByGranja($id_granja)
    {
        $table = Zend_Db_Table::getDefaultAdapter();
        $select = $table->select()
            ->from(array('ug' => 'usuario_has_granja'), array('*'))
            ->joinInner(array('g' => 'granja'), "ug.granja_id = g.id", array('nombre'))
            ->joinInner(array('u' => 'usuario'), "ug.usuario_id = u.id", array('correo'))
            ->where('ug.granja_id=' . $id_granja);

        $resultSet = $table->fetchAll($select);

        $entries = array();
        foreach ($resultSet as $row) {
            $mapped = $this->map($row);
            $mapped->addOtherColumn(array("granja" => $row['nombre']));
            $mapped->addOtherColumn(array("usuario" => $row['correo']));
            $entries[] = $mapped;
        }
        return $entries;
    }

    public function fetchByUsuario($id_usuario)
    {
        $table = Zend_Db_Table::getDefaultAdapter();
        $select = $table->select()
            ->from(array('ug' => 'usuario_has_granja'), array('*'))
            ->joinInner(array('g' => 'granja'), "ug.granja_id = g.id", array('nombre'))
            ->joinInner(array('u' => 'usuario'), "ug.usuario_id = u.id", array('correo'))
            ->where('ug.usuario_id=' . $id_usuario);

        $resultSet = $table->fetchAll($select);

        $entries = array();
        foreach ($resultSet as $row) {
            $mapped = $this->map($row);
            $mapped->addOtherColumn(array("granja" => $row['nombre']));
            $mapped->addOtherColumn(array("usuario" => $row['correo']));
            $entries[] = $mapped;
        }
        return $entries;
    }


    /**
     * @param $usuario_id
     * @param $granja_id
     * @return Application_Model_UsuarioGranja
     */
    public function findByUsuarioAndGranja($usuario_id, $granja_id)
    {
        $select = $this->getDbTable()->select()->where("usuario_id=" . $usuario_id . " AND granja_id=" . $granja_id);
        return $this->mapSelect($select);
    }

    public function delete($id_granja, $id_usuario)
    {
        $this->getDbTable()->delete('usuario_id=' . $id_usuario . ' AND granja_id=' . $id_granja);
    }

}